package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.domain.dto.ShelvedBookDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.ShelvedBook;
import com.ruoyi.service.IShelvedBookService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 上架Controller
 *
 * @author mjx
 * @date 2024-03-12
 */
@RestController
@RequestMapping("/book/shelved")
public class ShelvedBookController extends BaseController
{
    @Autowired
    private IShelvedBookService shelvedBookService;


    /**
     * 查询上架信息
     */
    @GetMapping("/getList")
    public TableDataInfo getList(ShelvedBookDto shelvedBookDto)
    {
        startPage();
        List<ShelvedBookDto> list = shelvedBookService.selectList(shelvedBookDto);
        return getDataTable(list);
    }

//    /**
//     * 查询图书信息
//     */
//    @GetMapping("/getList")
//    public TableDataInfo getBookList(ShelvedBook shelvedBook)
//    {
//        startPage();
//        List<ShelvedBook> list = shelvedBookService.selectBookList(shelvedBook);
//        return getDataTable(list);
//    }

    /**
     * 查询上架列表
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:list')")
    @GetMapping("/list")
    public TableDataInfo list(ShelvedBook shelvedBook)
    {
        startPage();
        List<ShelvedBook> list = shelvedBookService.selectShelvedBookList(shelvedBook);
        return getDataTable(list);
    }

    /**
     * 导出上架列表
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:export')")
    @Log(title = "上架", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ShelvedBook shelvedBook)
    {
        List<ShelvedBook> list = shelvedBookService.selectShelvedBookList(shelvedBook);
        ExcelUtil<ShelvedBook> util = new ExcelUtil<ShelvedBook>(ShelvedBook.class);
        util.exportExcel(response, list, "上架数据");
    }

    /**
     * 获取上架详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:query')")
    @GetMapping(value = "/{shelvedId}")
    public AjaxResult getInfo(@PathVariable("shelvedId") Long shelvedId)
    {
        return success(shelvedBookService.selectShelvedBookByShelvedId(shelvedId));
    }

    /**
     * 新增上架
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:add')")
    @Log(title = "上架", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ShelvedBook shelvedBook)
    {
        return toAjax(shelvedBookService.insertShelvedBook(shelvedBook));
    }

    /**
     * 修改上架
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:edit')")
    @Log(title = "编辑书架", businessType = BusinessType.UPDATE)
    @PostMapping("/{edit}")
    public AjaxResult edit(@RequestBody ShelvedBook shelvedBook)
    {
        return toAjax(shelvedBookService.updateShelvedBook(shelvedBook));
    }

    /**
     * 删除上架
     */
    @PreAuthorize("@ss.hasPermi('system:shelvedbooks:remove')")
    @Log(title = "上架", businessType = BusinessType.DELETE)
    @DeleteMapping("/{shelvedIds}")
    public AjaxResult remove(@PathVariable Long[] shelvedIds)
    {
        return toAjax(shelvedBookService.deleteShelvedBookByShelvedIds(shelvedIds));
    }


    @PostMapping("/upShelve")
    public AjaxResult upShelve(ShelvedBook shelvedBook){
        String msg = shelvedBookService.upShelve(shelvedBook);
        AjaxResult ajaxResult = new AjaxResult(200,msg);
        return ajaxResult;
    }

    @PostMapping("/offShelve")
    public AjaxResult offShelve(ShelvedBook shelvedBook){
        String msg = shelvedBookService.offShelve(shelvedBook);
        AjaxResult ajaxResult = new AjaxResult(200,msg);
        return ajaxResult;
    }

}
